package com.merelyb.data.service.service.mybatis.dao.base;

import com.merelyb.data.service.common.MultiDataSourceContextHolder;
import org.apache.commons.collections4.CollectionUtils;
import org.mybatis.spring.SqlSessionTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiM on 2016/7/25.
 */
public class BaseDao<T> {
    @Resource
    protected SqlSessionTemplate sqlSession;

    public BaseDao(){

    }

    /**
     * 查询所有
     * @param obj
     * @return
     * @throws Exception
     */
    public List<T> queryAllList(T obj, String sDataBase) throws Exception{
        MultiDataSourceContextHolder.setDataSourceType(sDataBase);
        return this.sqlSession.selectList(obj.getClass().getName() + ".select", obj);
    }

    /**
     * 分页查询
     * @param obj
     * @return
     */
    public List<T> queryListByLimit(T obj, String sDataBase){
        MultiDataSourceContextHolder.setDataSourceType(sDataBase);
        return null!=obj?this.sqlSession.selectList(obj.getClass().getName() + ".pageSelect", obj):null;
    }

    /**
     * 单条数据新增
     * @param obj
     */
    public void addData(T obj, String sDataBase) throws Exception{
        if(null != obj){
            ArrayList objs = new ArrayList();
            objs.add(obj);
            this.addData(objs, sDataBase);
        }
    }

    /**
     * 批量新增数据
     * @param objs
     */
    public void addData(List<T> objs, String sDataBase) throws Exception {
        if(CollectionUtils.isNotEmpty(objs)){
            MultiDataSourceContextHolder.setDataSourceType(sDataBase);
            this.sqlSession.insert(objs.get(0).getClass().getName() + ".addData", objs);
        }
    }

    /**
     * 更新单条数据
     * @param obj
     */
    public void updateData(T obj, String sDataBase) throws Exception{
        if(null != obj){
            MultiDataSourceContextHolder.setDataSourceType(sDataBase);
            this.sqlSession.update(obj.getClass().getName() + ".updateData", obj);
        }
    }

    /**
     * 逻辑删除数据
     * @param obj
     * @throws Exception
     */
    public void deleteData(T obj, String sDataBase) throws Exception{
        if (null != obj){
            MultiDataSourceContextHolder.setDataSourceType(sDataBase);
            this.sqlSession.update(obj.getClass().getName() + ".deleteData", obj);
        }
    }

    /**
     * 统计数量
     * @param obj
     * @return
     */
    public int getCount(T obj, String sDataBase){
        if (null != obj) {
            MultiDataSourceContextHolder.setDataSourceType(sDataBase);
            return sqlSession.selectOne(obj.getClass().getName() + ".getCount", obj);
        }else {
            return 0;
        }
    }

    /**
     * 物理删除数据
     * @param obj
     * @throws Exception
     */
    public void deleteDiskData(T obj, String sDataBase) throws Exception{
        if (null != obj){
            MultiDataSourceContextHolder.setDataSourceType(sDataBase);
            this.sqlSession.update(obj.getClass().getName() + ".deleteDiskData", obj);
        }
    }

    /**
     * 批量物理删除数据
     * @param objs
     * @throws Exception
     */
    public void deleteDiskDataAll(List<T> objs, String sDataBase) throws Exception{
        if(CollectionUtils.isNotEmpty(objs)){
            MultiDataSourceContextHolder.setDataSourceType(sDataBase);
            this.sqlSession.update(objs.get(0).getClass().getName() + ".deleteDiskDataAll", objs);
        }
    }
}
